# -*- coding: utf-8 -*-

## ------------------------------------------------------------
## Copyright 2010 Pierre-Antoine Delsart
## 
## This file is part of LeVin.
## 
## LeVin is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## LeVin is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with LeVin.  If not, see <http://www.gnu.org/licenses/>.
## ---------------------------------------------------------------

from PythonQt import QtCore, QtGui
from qtcommon import *
import tabcomponent
import wineBase


class TastingWidgetList(list):
    parent=None
    
    def init(self,parent):
        self.parent = parent
        self.verticalLayout = QtGui.QVBoxLayout(parent)        
    def resize(self,i):

        if len(self)>= i:
            return
        miss = i-len(self)
        import displayTasting
        for i in xrange(miss):
            wid = QtGui.QWidget(self.parent)
            widform = displayTasting.Ui_Form()
            widform.setupUi(wid)
            self.append(widform)
            self.verticalLayout.addWidget( wid)
tastingWidgetList = TastingWidgetList()


class WineTastingTab(tabcomponent.TabComponent):
    addTastingDialog = None
    def setupTab(self, tabWidget):
        tastingTab = QtGui.QWidget()
        tastingTab.setObjectName("tastingTab")


        verticalLayoutDesc = QtGui.QVBoxLayout(tastingTab)
        verticalLayoutDesc.setSpacing(20)
        verticalLayoutDesc.setObjectName("verticalLayoutDesc")
        self.verticalLayoutDesc = verticalLayoutDesc

        tastingWidget = QtGui.QWidget(tastingTab)
        verticalLayoutDesc.addWidget( tastingWidget)

        tastingWidgetList.init( tastingWidget)

        spacerItem = QtGui.QSpacerItem(20, 109, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)        
        verticalLayoutDesc.addItem(spacerItem)


        tabWidget.addTab(tastingTab, tr("Tasting"))
        self.tabId = tabWidget.indexOf(tastingTab)

        self.tastingWidgetList = tastingWidgetList
        
    def update(self,b):
        tastings = b.tastings
        self.tastingWidgetList.resize(len(tastings))
        print self.tastingWidgetList
        for wid, t in zip(self.tastingWidgetList, tastings):
            #print '   tasting ',t.year
            wid.label_year.setText(str(t.date))
            wid.label_rate.setPixmap(WinePixmaps.pixRates_small[t.rate])
            wid.label_rate.setToolTip(str(t.rate)+'/10')
            #wid.textBrowser.setText(t.comment if t.comment else "rien")
            wid.setTastingText(t)
            wid.wid.setVisible(True)
        for wid in self.tastingWidgetList[len(tastings):]:
            wid.wid.setVisible(False)

    def retranslate(self):
        pass

    def connect(self):
        tabcomponent.TabComponent.connect(self)        
        mainWindow = self.mainWindow        
        #connect(self.addTasting_button ,QtCore.SIGNAL("clicked()"), mainWindow.addRate )
        #connect(self.addTasting_button ,QtCore.SIGNAL("clicked()"), self.addTasting )



    def addTasting(self):
        print 'adding Tasting'
        row = self.current_row
        b = row.bottle
        addTastingDialog = self.addTastingDialog
        if addTastingDialog is None:
            print ' ** create tasting dialog'
            #addTastingDialog_w = QtGui.QDialog(self.mainWindow)
            import editTasting
            addTastingDialog = editTasting.Ui_Dialog()
            addTastingDialog.setupStandAlone( self.mainWindow.windowWidget)
            #addTastingDialog.Dialog  = addTastingDialog_w

            self.addTastingDialog = addTastingDialog
        res = addTastingDialog.saDialog.exec_()

        print 'dialog executed ...',res
        if not res:
            return

        d = addTastingDialog.currentValues()
        wineBase.addTasting(b,
                            **d
                            ## year = addTastingDialog.spinBox_year.valu(),
                            ## rate = addTastingDialog.spinBox_rate.value(),
                            ## comment = unicode(addTastingDialog.plainTextEdit.document().toPlainText() )
                            )

        self.mainWindow.bottleChanged(b)
        self.update(b)

    def editTasting(self):
        pass
    def removeTasting(self):
        pass
